package com.example.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet({"/logout"})
public class LogoutServlet extends HttpServlet {
    public LogoutServlet() {
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().invalidate();
        Cookie[] cookies = req.getCookies();
        if (cookies != null) {
            for(Cookie cookie : cookies) {
                if ("username".equals(cookie.getName())) {
                    System.out.println("\"name\" = name");
                    cookie.setValue("");
                    resp.addCookie(cookie);
                }

                if ("password".equals(cookie.getName())) {
                    System.out.println("\"pass\" = pass");
                    cookie.setValue("");
                    resp.addCookie(cookie);
                }
            }
        }

        resp.sendRedirect("login.jsp");
    }
}